package com.yh.wuye.view;

import java.util.ArrayList;

import com.yh.wuye.Entity.CallPayer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class CallPayMoneyAdapter extends BaseAdapter
{
  public interface OnCatalogOperator
  {
    void onCall(int idx);
  }
  
  private OnCatalogOperator mCatalogOperator;
  private ArrayList<Object> mList; 
  private Context mContext; 
  
  public CallPayMoneyAdapter(Context context,OnCatalogOperator onCatalogOperator)
  {
    super();
    mList = new ArrayList<Object>();
    mContext = context;
    mCatalogOperator = onCatalogOperator;
  }

  @Override
  public int getCount()
  {
    return mList.size();
  }
  
  public void setData(ArrayList<Object> list)
  {
    mList = list;
  }
  
  public void addItem(Object obj)
  {
    mList.add(obj);
  }
  
  /**删除
   * @param o
   */
  public void removeItem(int idx)
  {
    mList.remove(idx);
    //刷新
    this.notifyDataSetInvalidated();
  }
  
  public void clearItems()
  {
    mList.clear();
  }
  
  @Override
  public Object getItem(int position)
  {
    return mList.get(position);
  }

  @Override
  public long getItemId(int position)
  {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    CallPayer obj = (CallPayer)mList.get(position);
    
    Holder holder;
    if (convertView==null)
    {
      convertView = LayoutInflater.from(mContext).inflate(R.layout.call_pay_money_item, null);
      holder = new Holder();
      holder.btnCall = (Button) convertView.findViewById(R.id.btnCall);
      holder.btnCall.setOnClickListener(onCall);
      holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
      holder.tvTodayCount = (TextView) convertView.findViewById(R.id.tvTodayCount);
      holder.tvMobile = (TextView) convertView.findViewById(R.id.tvMobile);
      convertView.setTag(holder);
    }else
      holder = (Holder) convertView.getTag();

    holder.tvName.setText(obj.code);
    holder.tvTodayCount.setText(obj.money);
    holder.tvMobile.setText(obj.mobile);
    holder.btnCall.setTag(position);
    
    return convertView;
  }

  private class Holder
  {
    Button btnCall;
    TextView tvName;
    TextView tvTodayCount;
    TextView tvMobile;
  }
  
  private OnClickListener onCall = new OnClickListener()
  {
    
    @Override
    public void onClick(View v)
    {
      int idx = (Integer) v.getTag();
      mCatalogOperator.onCall(idx);
    }
  };
  
}
